package www.ld.com.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.ld.com.core.group.InsertGroup;
import www.ld.com.core.group.UpdateGroup;
import www.ld.com.domain.R;
import www.ld.com.domain.brand.pojo.BrandPO;
import www.ld.com.domain.brand.query.BrandQuery;
import www.ld.com.domain.brand.vo.BrandVO;
import www.ld.com.domain.brand.vo.InBrandVO;
import www.ld.com.domain.brand.vo.OutBrandVO;
import www.ld.com.domain.vo.PageRestVO;
import www.ld.com.service.BrandService;
import www.ld.com.utils.PageUtils;

import javax.annotation.Resource;

@Api(tags = "品牌模块")
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    @GetMapping
    @ApiOperation(value = "查询品牌列表")
    @PreAuthorize("hasAuthority('pms:brand:select')")
    public R<PageRestVO<BrandVO>> pageList(BrandQuery branderQuery) {
        PageRestVO pageRestVO = PageUtils.copyPageVO(brandService.pageBrandByBrandQuery(branderQuery), BrandVO.class);
        return R.ok(pageRestVO);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取品牌详情")
    @PreAuthorize("hasAuthority('pms:brand:select')")
    public R<OutBrandVO> getBrandById(@PathVariable(value = "id", required = true) String id) {
        BrandPO brandPO = brandService.getBrandPOById(id);
        return R.ok(BeanUtil.toBean(brandPO, OutBrandVO.class));
    }

    @ApiOperation(value = "新增品牌")
    @PostMapping
    @PreAuthorize("hasAuthority('pms:brand:create')")
    public R save(@RequestBody @Validated(value = {InsertGroup.class}) InBrandVO inBrandVO) {
        brandService.saveBrandPO(BeanUtil.toBean(inBrandVO, BrandPO.class));
        return R.ok();
    }

    @ApiOperation(value = "修改品牌")
    @PutMapping
    @PreAuthorize("hasAuthority('pms:brand:update')")
    public R update(@RequestBody @Validated(value = {UpdateGroup.class}) InBrandVO inUserVO) {
        brandService.updateBrandPO(BeanUtil.toBean(inUserVO, BrandPO.class));
        return R.ok();
    }

    @ApiOperation(value = "删除品牌")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('pms:brand:delete')")
    public R delete(@PathVariable(value = "id", required = true) String id) {
        brandService.deleteBrandPOById(id);
        return R.ok();
    }

}
